#include "groupmodel.hpp"
#include "db.hpp"

bool groupmodel::createGroup(group &gp)
{
    char sql[1024] = {0};
    sprintf(sql, "insert into allgroup(groupname, groupdesc) values('%s', '%s')", gp.getname().c_str(), gp.getdec().c_str());
    // 获取id
    Mysql mysql;
    if (mysql.connect())
    {
        if (mysql.update(sql))
        {
            // 设置group的id
            gp.setid(mysql_insert_id(mysql.getConnection()));
            return true;
        }
    }
    return false;
}

// 用户加入群组
bool groupmodel::addGroup(int userid, int groupid, string role)
{
    char sql[1024] = {0};
    sprintf(sql, "insert into groupuser values(%d, %d, '%s')", groupid, userid, role.c_str());
    Mysql mysql;
    if (mysql.connect())
    {
        if (mysql.update(sql))
        {
            return true;
        }
    }
    return false;
}

// 查询用户所在群组信息(用户可能存在多个群)
vector<group> groupmodel::queryGroups(int userid)
{
    vector<group> vec;
    char sql[1024] = {0};
    sprintf(sql, "select a.id,a.groupname,a.groupdesc from allgroup a inner join groupuser b on a.id = b.groupid where b.userid=%d", userid);
    Mysql mysql;
    if (mysql.connect())
    {
        MYSQL_RES *res = mysql.query(sql);
        if (res != nullptr)
        {
            MYSQL_ROW row;
            while ((row = mysql_fetch_row(res)) != nullptr)
            {
                group gp;
                gp.setid(atoi(row[0]));
                gp.setname(row[1]);
                gp.setdec(row[2]);
                vec.push_back(gp);
            }
            mysql_free_result(res);
        }
    }

    // 添加group的groupuser对象
    for (auto &gp : vec)
    {
        char sql[1024] = {0};
        sprintf(sql, "select a.id,a.name,a.state,b.grouprole from user a inner join groupuser b on b.userid = a.id where b.groupid=%d", gp.getid());
        Mysql mysql;
        if (mysql.connect())
        {
            MYSQL_RES *res = mysql.query(sql);
            if (res != nullptr)
            {
                MYSQL_ROW row;
                while ((row = mysql_fetch_row(res)) != nullptr)
                {
                    groupuser gpuser;
                    gpuser.setid(atoi(row[0]));
                    gpuser.setname(row[1]);
                    gpuser.setstate(row[2]);
                    gpuser.setrole(row[3]);
                    gp.getgroupuser().push_back(gpuser);
                }
                mysql_free_result(res);
            }
        }
    }
    return vec;
}

// 根据groupid查询群组用户信息，不包括user自己
// 用户群聊业务给群组其他人发送消息
vector<int> groupmodel::queryGroupUsers(int userid, int groupid)
{
    char sql[1024] = {0};
    sprintf(sql, "select userid from groupuser where groupid = %d and userid != %d", groupid, userid);
    vector<int> vec;
    Mysql mysql;
    if(mysql.connect())
    {
        MYSQL_RES *res = mysql.query(sql);
        if(res!=nullptr)
        {
            MYSQL_ROW row;
            while ((row = mysql_fetch_row(res)) != nullptr)
            {
                vec.push_back(atoi(row[0]));
            }
            mysql_free_result(res);
        }
    }
    return vec;
}
